//
//  ASDetailController.m
//  AvatarShow
//
//  Created by xunruiIOS on 2021/4/30.
//

#import "ASDetailController.h"
#import "ASHomeMainModel.h"
#import "GXCardView.h"
#import "ASDetailCardCell.h"
#import "ASDetailTopView.h"
#import "ASDetailBottomView.h"
#import "ASDetailUserContentView.h"
#import "ASHomeSearchListVC.h"
#import "ASHomeApi.h"

@interface ASDetailController () <GXCardViewDataSource, GXCardViewDelegate>

@property (strong, nonatomic) NSMutableArray *dataSource;
@property (strong, nonatomic) NSArray *oldArr;
@property (assign, nonatomic) NSInteger showIndex;
@property (strong, nonatomic) GXCardView *cardView;
@property (strong, nonatomic) ASDetailTopView *topView;
@property (strong, nonatomic) ASDetailBottomView *bottomView;
@property (strong, nonatomic) ASDetailUserContentView *contentView;
@property (assign, nonatomic) ASDetailCardType cardType;            ///卡片类型

@end

@implementation ASDetailController

- (void)dealloc{
    if (self.bottomView) {
        [self.bottomView saveCollectionData];	
    }
    [self.bottomView removeFromSuperview];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithHexString:@"fafafa"];
    self.title = @"详情";
    self.cardType = ASDetailCardType_Square;
    [self.view addSubview:self.topView];
    [self.view addSubview:self.bottomView];
    [self.view addSubview:self.contentView];
    [self.view addSubview:self.cardView];
    if (self.dataSource.count < 1 && [self.subID isNotBlank]) {
        [self loadMainData];
    }
    [self.cardView reloadData];
    [ASTool addUMTongJiKey:@"details" subKey:self.type>0?@"expression_detail":@"portrail_detail" titleStr:@"总浏览量"];

    // Do any additional setup after loading the view.
}

- (void)loadMainData{
    [MBProgressHUD showMessage:@"加载中..."];
    [ASHomeApi loadOneGroupData:self.subID Completion:^(ASHomeMainModel *  _Nonnull result) {
        [MBProgressHUD hideHUD];
        if (result.state.intValue == 1) {
            [self.dataSource removeAllObjects];
            self.dataSource = [NSMutableArray new];
            
            for (int x = 0; x < result.picture_urls.count; x++) {
                ASHomeMainModel *subM = [ASHomeMainModel new];
                subM.avatar = result.avatar;
                subM.userid = result.userid;
                subM.nickname = result.nickname;
                subM.descriptionStr = result.descriptionStr;
                subM.picture_urls = result.picture_urls;
                subM.tag_arr = result.tag_arr;
                subM.tID = result.tID;
                subM.thumb = result.picture_urls[x];
                [self.dataSource addObject:subM];
            }
            
            [self.cardView reloadData];
        }else{
            [MBProgressHUD showError:result.errorMsg];
        }
    } failure:^(NSError * _Nonnull error) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showError:FENETERROR];
    }];
}

- (ASDetailUserContentView *)contentView{
    if (!_contentView) {
        _contentView = [[ASDetailUserContentView alloc]initWithFrame:CGRectMake(0, SCREENH_HEIGHT - 56 - Height_SafeAreaBottom-130, SCREEN_WIDTH, 130)];
        _contentView.backgroundColor = self.view.backgroundColor;
        @weakify(self);
        _contentView.tagView.clickSubTagBlock = ^(NSString * _Nonnull tagStr) {
            @strongify(self);
            [ASTool addUMTongJiKey:@"details" subKey:self.type>0?@"expression_detail":@"portrail_detail" titleStr:@"标签区域"];
            [self pushSearchContent:tagStr];
        };
    }
    return _contentView;
}

- (ASDetailBottomView *)bottomView{
    if (!_bottomView) {
        _bottomView = [[ASDetailBottomView alloc]initWithFrame:CGRectMake(0, SCREENH_HEIGHT - 56 - Height_SafeAreaBottom, SCREEN_WIDTH, 56+Height_SafeAreaBottom)];
        _bottomView.backgroundColor = [UIColor colorWithHexString:@"ffffff"];
        _bottomView.type = self.type;
    }
    return _bottomView;
}

- (ASDetailTopView *)topView{
    if (!_topView) {
        _topView = [[ASDetailTopView alloc]initWithFrame:CGRectMake((SCREEN_WIDTH - 150)/2, Height_NavBar+15, 150, 40)];
        @weakify(self);
        _topView.clickSubBtnBlock = ^(NSInteger btnTag) {
            @strongify(self);
            self.cardType = btnTag;
            [ASTool addUMTongJiKey:@"details" subKey:@"portrail_detail" titleStr:btnTag>0?@"圆形":@"方形"];
            [self.cardView reloadDataFormIndex:self.showIndex];
        };
        if (self.type == 1) {
            _topView.hidden = YES;
        }
    }
    return _topView;
}

- (GXCardView *)cardView{
    if (!_cardView) {
        CGFloat top = self.topView.bottom+15;
//        if (self.type == 1) {
//            top = Height_NavBar+30;
//        }
        _cardView = [[GXCardView alloc]initWithFrame:CGRectMake(40, top, SCREEN_WIDTH - 80, SCREEN_WIDTH - 60)];
        _cardView.dataSource = self;
        _cardView.delegate = self;
        _cardView.visibleCount = 3;
        _cardView.lineSpacing = 15.0;
        _cardView.interitemSpacing = 10.0;
        _cardView.maxAngle = 15.0;
        _cardView.maxRemoveDistance = 100.0;
    //    self.cardView.isRepeat = YES; // 新加入
        [_cardView registerClass:[ASDetailCardCell class] forCellReuseIdentifier:ASDetailCardCellStr];
    }
    return _cardView;
}

- (void)createOldArr:(NSArray *)oldArr showIndex:(NSInteger)showIndex{
    self.oldArr = oldArr;
    NSArray *firstArr = [oldArr subarrayWithRange:NSMakeRange(0, showIndex)];
    NSArray *lastArr = [oldArr subarrayWithRange:NSMakeRange(showIndex, oldArr.count-(showIndex))];
    self.dataSource = [NSMutableArray arrayWithArray:lastArr];
    [self.dataSource appendObjects:firstArr];
}

#pragma mark - GXCardViewDataSource
-(NSInteger)numberOfCountInCardView:(GXCardView *)cardView{
    return self.dataSource.count;
}

- (GXCardViewCell *)cardView:(GXCardView *)cardView cellForRowAtIndex:(NSInteger)index{
    ASDetailCardCell *cell = [cardView dequeueReusableCellWithIdentifier:ASDetailCardCellStr];
    cell.type = self.cardType;
    if (self.dataSource.count > index) {
        ASHomeMainModel *model = self.dataSource[index];
        [cell.mainImage sd_setImageWithURL:[NSURL URLWithString:model.thumb]];
    }
    return cell;
}

- (void)cardView:(GXCardView *)cardView didRemoveCell:(GXCardViewCell *)cell forRowAtIndex:(NSInteger)index direction:(GXCardCellSwipeDirection)direction{
//    FELog(@"adsasdfa===%zd",index);
    if (self.dataSource.count > index) {
        ASHomeMainModel *model = self.dataSource[index];
        [self.dataSource addObject:model];
    }
}

- (void)cardView:(GXCardView *)cardView didDisplayCell:(GXCardViewCell *)cell forRowAtIndex:(NSInteger)index{
    FELog(@"adsasdfa===%zd",index);
    self.showIndex = index;
    if (self.dataSource.count > index) {
        ASHomeMainModel *model = self.dataSource[index];
        self.contentView.model = model;
        self.bottomView.model = model;
        [self addSeeTongJI:model.tID];
    }
}

///跳转搜索页面
- (void)pushSearchContent:(NSString *)content{
    NSArray *hisArr = [[NSUserDefaults standardUserDefaults]objectForKey:SEARCHHISTORYKEY];
    ASHomeSearchListVC *listVC = [ASHomeSearchListVC new];
    listVC.searchContent = content;
    listVC.historyArr = [NSMutableArray arrayWithArray:hisArr];
    listVC.hasUp = YES;
    listVC.type = 1;
    [self.navigationController pushViewController:listVC animated:YES];
    
    listVC.willBackBlock = ^(NSMutableArray * _Nonnull hisArr) {
        [[NSUserDefaults standardUserDefaults]setObject:[NSArray arrayWithArray:hisArr] forKey:SEARCHHISTORYKEY];
        [[NSUserDefaults standardUserDefaults]synchronize];
    };
}

///添加浏览量统计
- (void)addSeeTongJI:(NSString *)subID{
    [ASHomeApi addSeeTongji:subID completion:^(id  _Nonnull result) {
    } failure:^(NSError * _Nonnull error) {
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
